package backtrack;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author : Rookie
 * @description : 整数分解
 * @date : 2023/7/1
 */
public class IntegerDecompose {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("输入一个整数：");
            String s = scanner.nextLine();
            int anInt = Integer.parseInt(s);
            List<List<Integer>> res = new ArrayList<>();
            decompose(anInt,1,new ArrayList<>(),res);
            System.out.println(res);
        }
    }

    private static void decompose(int target, int startIndex, List<Integer> current,List<List<Integer>> result) {
        if (target == 0) {
            if (isContinuousNum(current)) {
                result.add(new ArrayList<>(current));
            }
            return;
        }

        for (int i = startIndex; i <= target; i++) {
            current.add(i);
            decompose(target - i,i + 1,current,result);
            current.remove(current.size() - 1);
        }
    }

    private static boolean isContinuousNum(List<Integer> current) {
        if (current.size() == 1) {
            return false;
        }
        boolean flag = true;
        for (int i = 0; i < current.size() - 1; i++) {
            if (current.get(i+1) - current.get(i) != 1) {
                flag = false;
                break;
            }
        }
        return flag;
    }
}
